import {substringFn} from "./public";
import viewWidthFn from "./screen.js";
import mapState1Img from "../static/mapState1Img.png";
import mapState2Img from "../static/mapState2Img.png";
import mapState3Img from "../static/mapState3Img.png";

var geoCoordMap = {
    '东北':[126.63,44.75],
    '华北':[115.48,42.56],
    '华东':[119.48,32.56],
    '华中':[113.09,30.52],
    '华南':[113.15,24.06],
    '西南':[100.06,30.57],
    '西北':[93.37,40.08],

};
let areaData = Object.keys(geoCoordMap)
var dataMap = [
    {name: '东北', value: 100},
    {name: '华北', value: 100},
    {name: '华东', value: 100},
    {name: '华中', value: 100},
    {name: '华南', value: 100},
    {name: '西南', value: 100},
    {name: '西北', value: 100},
];
let regionList = [
    ['黑龙江','吉林','辽宁'],
    ['北京','天津','山西','河北','内蒙古'],
    ['上海','江苏','浙江','安徽','福建','江西','山东','台湾'],
    ['河南','湖北','湖南',],
    ['广东','广西','海南','香港','澳门'],
    ['四川','贵州','云南','重庆','西藏'],
    ['陕西','甘肃','青海','宁夏','新疆'],
 ]

 var geoRegionData = function(){
    let arr = 
    [{"name":"黑龙江","itemStyle":{"normal":{"areaColor":"#0f2f64"}}},{"name":"吉林","itemStyle":{"normal":{"areaColor":"#023265"}}},{"name":"辽宁","itemStyle":{"normal":{"areaColor":"#013d5d"}}},{"name":"北京","itemStyle":{"normal":{"areaColor":"#0295A6"}}},{"name":"天津","itemStyle":{"normal":{"areaColor":"#027EA6"}}},{"name":"山西","itemStyle":{"normal":{"areaColor":"#05578B"}}},{"name":"河北","itemStyle":{"normal":{"areaColor":"#046093"}}},{"name":"内蒙古","itemStyle":{"normal":{"areaColor":"#025383"}}},{"name":"上海","itemStyle":{"normal":{"areaColor":"#1d3564"}}},{"name":"江苏","itemStyle":{"normal":{"areaColor":"#053d5c"}}},{"name":"浙江","itemStyle":{"normal":{"areaColor":"#1d3564"}}},{"name":"安徽","itemStyle":{"normal":{"areaColor":"#013e73"}}},{"name":"福建","itemStyle":{"normal":{"areaColor":"#0e236f"}}},{"name":"江西","itemStyle":{"normal":{"areaColor":"#0a3e86"}}},{"name":"山东","itemStyle":{"normal":{"areaColor":"#026480"}}},{"name":"台湾","itemStyle":{"normal":{"areaColor":"#112865"}}},{"name":"河南","itemStyle":{"normal":{"areaColor":"#034b72"}}},{"name":"湖北","itemStyle":{"normal":{"areaColor":"#084a82"}}},{"name":"湖南","itemStyle":{"normal":{"areaColor":"#0d3679"}}},{"name":"广东","itemStyle":{"normal":{"areaColor":"#031e75"}}},{"name":"广西","itemStyle":{"normal":{"areaColor":"#06267c"}}},{"name":"海南","itemStyle":{"normal":{"areaColor":"#151653"}}},{"name":"香港","itemStyle":{"normal":{"areaColor":"#031e75"}}},{"name":"澳门","itemStyle":{"normal":{"areaColor":"#031e75"}}},{"name":"四川","itemStyle":{"normal":{"areaColor":"#073175"}}},{"name":"贵州","itemStyle":{"normal":{"areaColor":"#0f2a79"}}},{"name":"云南","itemStyle":{"normal":{"areaColor":"#06257b"}}},{"name":"重庆","itemStyle":{"normal":{"areaColor":"#0a4589"}}},{"name":"西藏","itemStyle":{"normal":{"areaColor":"#0b2162"}}},{"name":"陕西","itemStyle":{"normal":{"areaColor":"#004674"}}},{"name":"甘肃","itemStyle":{"normal":{"areaColor":"#034375"}}},{"name":"青海","itemStyle":{"normal":{"areaColor":"#013674"}}},{"name":"宁夏","itemStyle":{"normal":{"areaColor":"#015081"}}},{"name":"新疆","itemStyle":{"normal":{"areaColor":"#112b60"}}}]

    return arr
}

var geoRegionDataMap3 = function(){
    let arr = 
    [{"name":"黑龙江","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"吉林","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"辽宁","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"北京","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"天津","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"山西","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"河北","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"内蒙古","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"上海","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"江苏","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"浙江","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"安徽","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"福建","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"江西","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"山东","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"台湾","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"河南","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"湖北","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"湖南","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"广东","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"广西","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"海南","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"香港","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"澳门","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"四川","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"贵州","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"云南","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"重庆","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"西藏","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"陕西","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"甘肃","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"青海","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"宁夏","itemStyle":{"normal":{"areaColor":"#000A39"}}},{"name":"新疆","itemStyle":{"normal":{"areaColor":"#000A39"}}}]

    return arr
}

let colorData = ["#04D8F0", "#06C3EC", "#02BFFA" ,"#26A1EF", "#0092E8" ,"#00A7FF" ,"#03AEEF" ,"#178BFF"]
//各区定点信息数据
var jsonCoordArea = {
    data: [
        {
        areaName: '东北区',
        value: 94,
        lineS: [126.63,44.75],

    },
        {
        areaName: '华北区',
        value: 94,
        lineS: [115.48,42.56],
 
    },
        {
        areaName: '华东区',
        value: 94,
        lineS: [119.48,32.56],

    },
        {
        areaName: '华中区',
        value: 94,
        lineS: [113.09,30.52],

    },
    {
        areaName: '华南区',
        value: 90,
        lineS: [113.15,24.06],

    },
    {
        areaName: '西南区',
        value: 90,
        lineS: [100.06,30.57],

    },
    {
        areaName: '西北区',
        value: 90,
        lineS: [93.37,40.08],

    },
]
};

const deepCopyFn = (data)=>{
    if(data){
        return JSON.parse(JSON.stringify(data));
    }else{
        return null;
    }
}
const seriesList = (data,start,end)=>{
    return data.slice(start,end)
  }

const drawEffectScatter =(dataMap,effectScatter,geoCoordMap,convertData,option)=>{
    
 
    let neweffectScatter = effectScatter;
    neweffectScatter.data = convertData(dataMap,geoCoordMap)
    neweffectScatter.symbolSize= function (val) {
        return val[2] / 12;   
     }
     option.series.push(neweffectScatter)
}
// 点的动画方法
const showEffectScatterLegend = (color,name,option,dataMap,effectScatter,convertData,geoCoordMap,i)=>{
    // option.series=seriesList(option.series,0,3)
    let neweffectScatter = deepCopyFn(effectScatter);
    neweffectScatter.name = name;
    // neweffectScatter.label.normal.formatter=function(dataMap){
    //     return  dataMap.data.percent;
    // }
    neweffectScatter.itemStyle={normal:{
        color: color,

        opacity:name.opacity
    }}

    // if(!dataMap.length){
    //     neweffectScatter.itemStyle.normal.opacity=0.1
    // }    
    if(i==0){
        neweffectScatter.symbol='image://'+window.location.origin+`${mapState1Img}`;
    }else if(i==1){
        neweffectScatter.symbol='image://'+window.location.origin+`${mapState2Img}`;
    }else if(i==2){
        neweffectScatter.symbol='image://'+window.location.origin+`${mapState3Img}`;
    }
    neweffectScatter.data = convertData(dataMap,geoCoordMap)
    neweffectScatter.symbolSize= function (val) {
        let value = val[2] 
        // if(value>40){
        //     // value = 5+value%40;
        // }
        // console.log(value)
        return value;
    }

    option.series.push(neweffectScatter)
}
// 点的动画方法
const showEffectScatter = (times,option,dataMap,effectScatter,convertData,geoCoordMap)=>{
    option.series=seriesList(option.series,0,3)
       
    let neweffectScatter = deepCopyFn(effectScatter);
    neweffectScatter.itemStyle={normal:{
        color: '#18E5C9',
        shadowBlur: 10,
        shadowColor: '#333',
        // opacity:1
    }}
    neweffectScatter.data = convertData(dataMap,geoCoordMap)

    option.series.push(neweffectScatter)
}
// 线的动画方法
const showLines = (times,option,lines,jsonCoordArea,convertLineData)=>{
    option.series=seriesList(option.series,0,2)
    // if(option1.series.length>8){
    //  option1.series=seriesList(option1.series,0,2)

    // }

    let neweffectScatter = deepCopyFn(lines);
    neweffectScatter.effect={
            show: true,
            period: 10,
            color: 'rgb(122,215,241)',
            // color:'#042865',
            symbolSize: 8,
            loop:false
        }
    neweffectScatter.lineStyle.normal.opacity = 1
    neweffectScatter.data = convertLineData([jsonCoordArea.data[times]],times)
    // neweffectScatter.data = convertLineData(jsonCoordArea.data)
    option.series.push(neweffectScatter)
}
//将定点位置做一个碰撞检测，给其他组件
const offsetForOther = (offset,offsetTop,ScreenTableHeight,ScreenTableWidth,containerHeight,containerWidth,i)=>{
    let fontsize = slicePxFn(viewWidthFn())
    console.log(ScreenTableHeight,"ScreenTableHeight")
        offset.top = offset.top + (132.5+10)*fontsize/100;
        if(offset.top+ScreenTableHeight+offsetTop>containerHeight){
            offset.top = offset.top-ScreenTableHeight-(132.5+10+28)*fontsize/100
        }
        if(offset.left<ScreenTableWidth){
            if(offset.left>ScreenTableWidth/2){
                offset.left = ScreenTableWidth/3.5
            }else{
                offset.left = 0;
            }
        }
        if(offset.left+ScreenTableWidth>containerWidth){
            offset.left = containerWidth-ScreenTableWidth
        }
        if(i==2){
            offset.left = 600;
    
        }
    
    offset.left = offset.left*fontsize/100;

}
//屏幕坐标转换成地理坐标算法

var coordChange = (offsetf,containerWidth,containerHeight,circleWidth=10)=>{
    let offset = deepCopyFn(offsetf)

    let geoCenter = {e:104.6,n:36.05};//中心点的地理位置坐标
    let pageCenter = {x:containerWidth/2,y:containerHeight/2};
    // let pageCenter = {x:window.innerWidth/2,y:window.innerHeight/2};

    let lineMx = 5
    if(offset.left<pageCenter.x){
        offset.left =offset.left //+offset.offset+circleWidth/2

        lineMx = -5
    }else{
        offset.left =offset.left //+offset.offset-circleWidth/2

    }
    
    let x =(offset.left-pageCenter.x)*62/window.viewRectCop.width+geoCenter.e;
    let y = (pageCenter.y-offset.top)*35.24/window.viewRectCop.height+geoCenter.n;
    let lineE = [x,y]
    let lineM = [x-lineMx,y]
    console.log(containerWidth,offset.left-pageCenter.x,"XXX",window.viewRectCop.width,window.viewRectCop.height)
    return {lineM:lineM,lineE:lineE}
}

var convertLineData = function (data,times=0,opacity=0.4) {
    var res = [];

    for (var i = 0; i < data.length; i++) {
        if(!data[i]){
            continue;
        }
        var dataItem = data[i];
        var coordS = dataItem.lineS; //线起点
        var coordM = dataItem.lineM; //线中间点
        var coordE = dataItem.lineE; //线尾点
        let color = {
            type: 'linear',
            x: 1,
            y: 0,
            x2: 0,
            y2: 1,
        colorStops: [{
           offset: 0,
        color: '#00E3FF'
        }, // 0% 处的颜色
        {
          offset: 1,
           color: '#FFD000'
        } // 100% 处的颜色
        ],
           globalCoord: false // 缺省为 false
        }
        if(i>2 || times>2){
            color.y=1;
            color.y2=0;

        }
        if(i==4 || times==4){
            color.x=0;
            color.x2=1;
            color.y=1;
            color.y2=0;
        }
        if(i>4|| times>4){
            color.x=0;
            color.x2=1;
            color.y=0;
            color.y2=1;
        }

        if (coordS && coordM && coordE) {
            res.push({
                coords: [coordS, coordM, coordE],
                lineStyle:{
                    normal:{

                        color: color,
                        // opacity:opacity
                    }
                }
            });
        }
    }
    // console.log(res)
    return res;
};

var slicePxFn = (str) =>{
    if(typeof str == 'string'){
        return str.slice(0,str.length-2)
    }else{
        return str
    }
}
const effectScatterShadowBlur = (effectScatter)=>{
    let fontsize = slicePxFn(viewWidthFn());
    effectScatter.rippleEffect.scale = effectScatter.rippleEffect.scale*fontsize/100;
    
}
export {
    geoCoordMap,
    areaData,
    dataMap,
    regionList,
    geoRegionData,
    geoRegionDataMap3,
    colorData,
    jsonCoordArea,
    deepCopyFn,
    showEffectScatter,
    showLines,
    offsetForOther,
    coordChange,
    convertLineData,
    slicePxFn,
    showEffectScatterLegend,
    drawEffectScatter,
    effectScatterShadowBlur
};


